package login

import (
	"OnlineRetailers/controllers/errorDeal"
	error2 "OnlineRetailers/models/errorDeal"
	"bytes"
	"github.com/astaxie/beego"
	"github.com/dchest/captcha"
)

type GetCaptcha struct {
	beego.Controller
}

func (c *GetCaptcha) GetCaptcha() {

	defer func() {

		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Session")
		c.Ctx.ResponseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
		c.Ctx.ResponseWriter.Header().Set("Pragma", "no-cache")
		c.Ctx.ResponseWriter.Header().Set("Expires", "0")
		c.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
		c.ServeJSON()
	}()
	var addr = "/login/GetCaptcha"

	//captchaRes:=captcha.Server(captcha.StdWidth, captcha.StdHeight)
	var content bytes.Buffer
	log := &error2.Log{}

	id := c.GetString("id")
	//fmt.Println(captchaRes)
	err := captcha.WriteImage(&content, id, captcha.StdWidth, captcha.StdHeight)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(content)
	c.Ctx.Output.ContentType("png")
	err = c.Ctx.Output.Body(content.Bytes())
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
}
